﻿using System.Collections.Generic;

namespace Bilibili.Model.Common
{
    public sealed class DataSourceHeader
    {
        public string Referer { get; set; }
        public string UserAgent { get; set; }
        public DataSourceHeader(string referer, string userAgent)
        {
            Referer = referer;
            if (string.IsNullOrEmpty(Referer))
                Referer = "https://www.bilibili.com/";
            UserAgent = userAgent;
        }
        private Dictionary<string, string> ExtHeaders { get; set; }
        public Dictionary<string, string> ExtensionHeaders => ExtHeaders;
        public void Add(string key, string value)
        {
            if (ExtHeaders == null)
                ExtHeaders = new Dictionary<string, string>();
            ExtHeaders.Add(key, value);
        }
        public Dictionary<string, string> ToDictionary()
        {
            var dict = new Dictionary<string, string>();
            try
            {
                dict.Add("Referer", Referer);
                dict.Add("User-Agent", UserAgent);
                if (ExtHeaders != null && ExtHeaders.Count > 0)
                {
                    foreach (var ext in ExtHeaders)
                        dict.Add(ext.Key, ext.Value);
                }
            }
            catch { }
            return dict;
        }
    }
}